<template>
  <div class="fromEle">
    <!-- 上方盒子 -->
    <div class="leftBox"></div>
    <!-- 下方盒子 -->
    <div class="rigthBox">
      <van-form @submit="onSubmit" class="fromBox">
        <van-field
          v-model="username"
          name="username"
          label="用户名"
          placeholder="用户名"
          @blur="inputEve1"
          @input="inputBtn"
          ref="username"
        />
        <van-field
          v-model="password"
          type="password"
          name="password"
          label="密码"
          placeholder="密码"
          @blur="inputEve2"
          ref="password"
          @input="inputBtn"
        />
        <van-field
          v-model="confirmPassword"
          type="password"
          label="确认密码"
          name="password"
          placeholder="确认密码"
          @blur="inputEve3"
          ref="conPassword"
          @input="inputBtn"
        />
        <div class="submitBox">
          <van-button
            round
            block
            type="info"
            native-type="submit"
            class="submitBtn"
            :disabled="temp"
            >提交</van-button
          >
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
import { Toast } from "vant";
export default {
  data() {
    return {
      username: "", // 用户名
      password: "", // 密码
      confirmPassword: "", // 确认密码
      temp: true, // 按钮禁用状态
    };
  },
  methods: {
    // 注册用户
    onSubmit(values) {
      let info = {
        type: "post",
        url: "/user/register",
        head: "application/x-www-form-urlencoded",
        data: values,
      };
      this.$netWork(info).then((res) => {
        console.log(res);
        if (res.data.status === 0) {
          Toast(res.data.message);
          this.$router.push("/loginPage");
        } else {
          Toast(res.data.msg);
        }
      });
    },
    // 输入框输入事件
    inputEve1() {
      if (!this.$refs.username.value.trim()) {
        Toast("用户名不可为空");
        return;
      }
    },
    inputEve2() {
      if (!this.$refs.password.value.trim()) {
        Toast("密码不可为空");
        return;
      }
    },
    inputEve3() {
      if (
        !this.$refs.conPassword.value.trim() ||
        this.password != this.confirmPassword
      ) {
        Toast("密码不一致");
        return;
      }
    },
    // 提交按钮是否禁用
    inputBtn() {
      if (
        this.username &&
        this.password &&
        this.confirmPassword &&
        this.password == this.confirmPassword
      ) {
        this.temp = false;
      } else {
        this.temp = true;
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.fromEle {
  display: flex;
  flex-direction: column;
  align-items: center;
  height: 80%;

  // 上方盒子
  .leftBox {
    width: 100%;
    box-sizing: border-box;
    padding: 0;
  }

  // 下方盒子
  .rigthBox {
    width: 100%;
    .submitBox {
      margin: 25px 16px;
      .submitBtn {
        background: #f63515;
        border-color: #f63515;
      }
    }
  }

  // 输入框盒子
  .registerBox {
    display: flex;
    align-items: center;
    width: 100%;
    height: 40px;
    padding-left: 20px;
    border-top: 1px solid #ccc;
    border-bottom: 1px solid #ccc;
    border-radius: 40px;
    margin: 10px 0;
    input {
      width: 70%;
      height: 60%;
      border: none;
      text-decoration: none;
    }
  }
}
</style>
